
** Spatial approach, 5: Generate Lagged Sales ** 

	**********------------------------------**********
	* Contents: 
		* Build lagged sales variables for spatial regressions
		* Pre-condition for spatial transformation 
		* Input: cell-level data based on spatial_2_collapse_mc 
		* Output: final data for regression: spatial_6_regs_s_monthly 


capture log close 
clear all
set more off 

cd "R:\WSV2\TBu_AKe\Spatial_NEW"

capture mkdir Data
global store "R:\WSV2\TBu_AKe\Spatial_NEW\Data"

log using spatial_5_transform_s_2703, replace 

use "R:\WSV2\TBu_AKe\Spatial_NEW\spatial_2_collapse_mc.dta" 


drop if ccode == 0 // from collapse structure   

sort cell 

gsort -bunching +cell 
gen bcell = _n if bunching == 1 // identify B

gsort -restricted +cell 
gen rcell = _n if restricted == 1 // identify R

egen xc = group(ccode cell) //  
egen kt = group(ccode date) // 


******************************
*** prepare for regression *** 
******************************

** run up min-req-curve and find neighbors for each point 
		* i : 50 cells in bunching region, cells 1-50 in data 
		* j : restricted cells, 50 upwards 

rename cell_date_sales csd // simplify notation 
label variable csd "cell_date_sales"
rename cell_date_count ccd	
label variable ccd "cell_date_count"


sort cell 
xtset cell kt // crucial.  

gen log_sd = ln(csd)
gen log_cd = ln(ccd)
	
foreach i of numlist 12 24 36 48 60 {
	local k = `i'/12
	generate S`k'_lsd = S`i'.log_sd // takes S, bc D operator compounds. 
	generate S`k'_lcd = S`i'.log_cd
	generate L`k'_lsd = L`i'.log_sd // takes S, bc D operator compounds. 
	generate L`k'_lcd = L`i'.log_cd
}

foreach i of numlist  12 24 36 48 60 {
	local k = `i'/12
	generate S`k'_sd = S`i'.csd 
	generate S`k'_cd = S`i'.ccd
	generate L`k'_sd = L`i'.csd 
	generate L`k'_cd = L`i'.ccd
}
	
cd "$store"
save spatial_6_regs_s_monthly, replace // data set used for estimation 

log close 
clear 
exit 